<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核人" prop="shrPerson">
        <el-input v-model="queryParams.shrPerson" placeholder="请输入审核人" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="检查日期" prop="jcrq">
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.jcrq" type="date"
          value-format="yyyy-MM-dd" placeholder="选择检查日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检查人" prop="jcrPerson">
        <el-input v-model="queryParams.jcrPerson" placeholder="请输入检查人" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['Productionline_hygiene_main:hygiene_main:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['Productionline_hygiene_main:hygiene_main:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['Productionline_hygiene_main:hygiene_main:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['Productionline_hygiene_main:hygiene_main:export']"
            >导出</el-button
          >
        </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-s-check" size="mini" :disabled="multiple" @click="handleCheck"
          v-hasPermi="['Productionline_hygiene_main:hygiene_main:check']">审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hygiene_mainList" @selection-change="handleSelectionChange" max-height="550">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />

      <el-table-column label="检查日期" align="center" prop="jcrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jcrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="报表编号" align="center" prop="bbbh" /> -->
      <el-table-column label="检查人" align="center" prop="jcrPerson" />
      <el-table-column label="审核人" align="center" prop="shrPerson" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="250px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="excelhandleView(scope.row)">导出</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_hygiene_main:hygiene_main:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_hygiene_main:hygiene_main:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改仰口生产基地卫生、健康检查记录对话框 -->
    <el-dialog v-myFullscreen :title="title" :visible.sync="open" append-to-body width="700px" fullscreen>
      <!-- <TableHeader
          @nameChange="nameChange"
          :data="{
            mainTitle: '仰口生产基地卫生、健康检查记录',
            company: '青岛崂山矿泉水有限公司',
            code: form.bbbh,
          }"
        /> -->
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <!-- <el-col :span="12">
                  <el-form-item label="审核人" prop="shrPerson">
                    <el-input v-model="form.shrPerson" placeholder="请输入审核人" :disabled="openDisabled" />
                  </el-form-item>
              </el-col> -->
          <el-col :span="6">
            <el-form-item label="检查日期" prop="jcrq">
              <el-date-picker :editable="false" clearable v-model="form.jcrq" type="date" value-format="yyyy-MM-dd"
                placeholder="选择检查日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12">
                  <el-form-item label="检查人" prop="jcrPerson">
                    <el-input v-model="form.jcrPerson" placeholder="请输入检查人" :disabled="openDisabled" />
                  </el-form-item>
              </el-col> -->
          <!-- <el-col :span="12">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" :disabled="openDisabled" />
                  </el-form-item>
              </el-col> -->
        </el-row>
        <el-divider content-position="center">仰口生产基地卫生、健康检查记录信息</el-divider>
        <br />
        <br />
        <br />
        <br />
        <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQualityProdHygieneSub " :disabled="openDisabled">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQualityProdHygieneSub" :disabled="openDisabled">删除</el-button>
            </el-col>
          </el-row> -->
        <el-table class="noGao" :data="qualityProdHygieneSubList" :row-class-name="rowQualityProdHygieneSubIndex"
          ref="qualityProdHygieneSub" :span-method="objectSpanMethod" border max-height="550">
          <!-- <el-table-column type="selection" width="0" align="center" /> -->
          <!-- <el-table-column label="序号" align="center" prop="index" width="50"/> -->
          <!-- <el-table-column label="检查内容ID" prop="jcnrId">
              <template slot-scope="scope">
                  <el-input v-model="scope.row.jcnrId" placeholder="请输入检查内容ID" :disabled="openDisabled"/>
              </template>

            </el-table-column> -->

          <el-table-column label="检查类别" prop="jclbId" width="150px">
            <template slot-scope="scope">
              <el-select v-model="scope.row.jclbId" placeholder="请选择检查类别" style="width: 100%" :disabled="true">
                <el-option v-for="dict in Productionline_Hygiene_LB" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="检查内容" prop="jcnrId" min-width="450px">
            <template slot-scope="scope">
              <el-select v-model="scope.row.jcnrId" placeholder="请选择检查内容" style="width: 100%" :disabled="openDisabled">
                <el-option v-for="dict in Productionline_Hygiene" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <!-- <el-table-column label="其他形态" prop="jcnrId">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.jcnrId"
                  placeholder="请选择铜绿"
                  style="width: 100%"
                  :disabled="openDisabled"
                >
                  <el-option
                    v-for="dict in Productionline_Hygiene"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </template>

            </el-table-column> -->

          <!-- <template label="是否合格">
              <el-select v-model="value" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template> -->

          <el-table-column label="是否合格" prop="sfhg" min-width="150px">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.sfhg" placeholder="请输入是否合格" :disabled="openDisabled"/> -->
              <el-select v-model="scope.row.sfhg" :disabled="openDisabled">
                <el-option value="合格">合格</el-option>
                <el-option value="不合格">不合格</el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="整改期限" prop="zgrq" min-width="150px">
            <template slot-scope="scope">
              <el-input v-model="scope.row.zgrq" placeholder="请输入整改期限" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="复查结果" prop="fcjg" min-width="150px">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcjg" placeholder="请输入复查结果" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="检查日期" prop="jcrq"  width="150px">
              <template slot-scope="scope">
                  <el-date-picker  :editable="false" clearable size="small" v-model="scope.row.jcrq" type="date"  value-format="yyyy-MM-dd HH:mm:SS"  placeholder="选择检查日期" :disabled="openDisabled">
                  </el-date-picker>
              </template>
            </el-table-column> -->
          <!-- <el-table-column label="备注" prop="remark"  width="150px">
              <template slot-scope="scope">
                  <el-input v-model="scope.row.remark" placeholder="请输入备注" :disabled="openDisabled"/>
              </template>
            </el-table-column> -->
        </el-table>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核人" prop="shrPerson">
              <el-input v-model="form.shrPerson" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查人" prop="jcrPerson">
              <!-- <el-input v-model="form.jcrPerson" placeholder="请输入检查人" :disabled="openDisabled" /> -->
              <el-select v-model="form.jcrPerson" filterable allow-create style="width: 100%" :disabled="openDisabled">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName"
                  :value="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div>
        备注1、合格划“√”，不合格写明不合格情况，不合格项需写明整改期限及整改后复查结果；
        2、对违规员工，初犯者予以警告，累犯者依据《员工手册》给予相应处罚。
      </div>

      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</div>
</template>

<script>
import {
  getInspectionSub_main,
  listHygiene_main,
  getHygiene_main,
  delHygiene_main,
  addHygiene_main,
  updateHygiene_main,
  checkHygiene_main
} from "@/api/quality/productionline/hygiene_main";
import { getInfo } from "@/api/login";
import { listUser } from "@/api/system/user";
export default {
  name: "Hygiene_main",
  data() {
    return {
      // options: [{
      //     value: '选项1',
      //     label: '黄金糕'
      //   }, {
      //     value: '选项2',
      //     label: '双皮奶'
      //   }, {
      //     value: '选项3',
      //     label: '蚵仔煎'
      //   }, {
      //     value: '选项4',
      //     label: '龙须面'
      //   }, {
      //     value: '选项5',
      //     label: '北京烤鸭'
      //   }],
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQualityProdHygieneSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仰口生产基地卫生、健康检查记录表格数据
      hygiene_mainList: [],
      // 仰口生产基地卫生、健康检查记录表格数据
      qualityProdHygieneSubList: [],
      Productionline_Hygiene: [],
      Productionline_Hygiene_LB: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shrPerson: null,
        jcrq: null,
        jcrPerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      /* 当前用户名 */
      nickName: "",
      userList: [],
    };
  },
  created() {
    this.getList();
    this.queryUserList();
    this.getDicts("Productionline_Hygiene_LB").then((response) => {
      console.log(response)
      this.Productionline_Hygiene_LB = response.data;
    });
    this.getDicts("Productionline_Hygiene").then((response) => {
      this.Productionline_Hygiene = response.data;
    });
  },
  methods: {
    /** 查询仰口生产基地卫生、健康检查记录列表 */
    getList() {
      this.loading = true;
      listHygiene_main(this.queryParams).then((response) => {
        this.hygiene_mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getInspectionSub_main() {
      getInspectionSub_main().then((response) => { });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shrPerson: null,
        jcrq: null,
        bbbh: "LK-QF-LS1-10-015-11-05",
        jcrPerson: this.nickName,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.qualityProdHygieneSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加仰口生产基地卫生、健康检查记录";

      this.handleAddQualityProdHygieneSub();
      // this.qualityProdHygieneSubList = [];

      // this.qualityProdHygieneSubList = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getHygiene_main(id).then((response) => {
        this.form = response.data;
        this.qualityProdHygieneSubList =
          response.data.qualityProdHygieneSubList;
        this.open = true;
        this.title = "修改仰口生产基地卫生、健康检查记录";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getHygiene_main(id).then((response) => {
        this.form = response.data;
        this.qualityProdHygieneSubList =
          response.data.qualityProdHygieneSubList;
        this.open = true;
        this.title = "查看仰口生产基地卫生、健康检查记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
          this.form.qualityProdHygieneSubList = this.qualityProdHygieneSubList;
          /**打开遮罩层*/
          const rLoading = this.openLoading();
          if (this.form.id != null) {
            updateHygiene_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            addHygiene_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口生产基地卫生、健康检查记录编号为"' +
        ids +
        '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delHygiene_main(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 仰口生产基地卫生、健康检查记录序号 */
    rowQualityProdHygieneSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 仰口生产基地卫生、健康检查记录添加按钮操作 */
    handleAddQualityProdHygieneSub() {
      for (let i = 1; i <= 28; i++) {
        let obj = {};
        obj.jclbId = i + "";
        obj.jcnrId = i + "";
        obj.sfhg = "合格";
        obj.zgrq = "";
        obj.fcjg = "";
        obj.jcrq = "";
        obj.remark = "";
        this.qualityProdHygieneSubList.push(obj);
      }
      // this.open = true;
      // this.title = "修改仰口生产基地卫生、健康检查记录";
      // console.log(this.qualityProdHygieneSubList);

      // this.open = true;
      // this.title = "修改仰口生产基地卫生、健康检查记录";
      // console.log(this.qualityProdHygieneSubList);
    },
    /** 仰口生产基地卫生、健康检查记录删除按钮操作 */
    handleDeleteQualityProdHygieneSub() {
      if (this.checkedQualityProdHygieneSub.length == 0) {
        this.$alert(
          "请先选择要删除的仰口生产基地卫生、健康检查记录数据",
          "提示",
          { confirmButtonText: "确定" }
        );
      } else {
        this.qualityProdHygieneSubList.splice(
          this.checkedQualityProdHygieneSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleQualityProdHygieneSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.qualityProdHygieneSub.clearSelection();
        this.$refs.qualityProdHygieneSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedQualityProdHygieneSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_hygiene_main/hygiene_main/export",
        {
          ...this.queryParams,
        },
        "仰口生产基地卫生、健康检查记录" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    excelhandleView(row) {//Boolean isVertical,int isTitle,Boolean autoAddLine
      //isVertical:true为竖表；false为横表。竖表时isTitle和autoAddLine参数无意义
      //isTitle:1为自动添加标题，{+sub_startlog+}所在行为标题；0为不添加标题，{+sub_startlog+}所在行为第一条数据。
      //autoAddLine:true为自动添加新行；false不添加新行。
      this.download(
        "Productionline/hygiene_main/exportexcel?isVertical=false&isTitle=0&autoAddLine=false&id=" + row.id,
        {
          ...this.queryParams,
        },
        "仰口生产基地卫生健康检查记录" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
    /** 审核按钮操作 */
    handleCheck(row) {
      /* 审核 */
      this.ids.forEach((id) => {
        getHygiene_main(id).then((response) => {
          if (
            response.data.shrPerson != null &&
            response.data.shrPerson != ""
          ) {
            this.msgError(
              '编号为"' + id + '"的数据项已审核完成，无需重复审核！'
            );
          } else {
            let obj = {};
            obj.id = id;
            obj.shrPerson = this.nickName;
            obj.qualityProdHygieneSubList = this.qualityProdHygieneSubList;
            /* 修改 */
            checkHygiene_main(obj).then((response) => {
              this.msgSuccess('编号为"' + id + '"的数据项审核成功！');
              this.getList();
            });
          }
        });
      });
    },
    /* 查询用户列表 */
    queryUserList() {
      listUser().then((response) => {
        this.userList = response.rows;
      });
      getInfo().then((response) => {
        this.nickName = response.user.nickName;
      });
    },
    /* 合并列 */
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        // if (rowIndex % 2 === 0) {
        //   return {
        //     rowspan: 2,
        //     colspan: 1
        //   };
        // } else {
        //   return {
        //     rowspan: 0,
        //     colspan: 0
        //   };
        // }
        if (columnIndex === 0) {
          const _row = this.flitterData(this.qualityProdHygieneSubList).one[
            rowIndex
          ];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col,
          };
        }
      }
    },
    flitterData(arr) {
      let spanOneArr = [],
        concatOne = 0;
      arr.forEach((item, index) => {
        if (index === 0) {
          spanOneArr.push(1);
        } else {
          if (
            this.selectDictLabel(
              this.Productionline_Hygiene_LB,
              item.jclbId
            ) ===
            this.selectDictLabel(
              this.Productionline_Hygiene_LB,
              arr[index - 1].jclbId
            )
          ) {
            //  this.selectDictLabel(this.statusOptions, row.status)
            // if (item.jclbId === arr[index - 1].jclbId) {
            //第一列需合并相同内容的判断条件
            spanOneArr[concatOne] += 1;
            spanOneArr.push(0);
          } else {
            spanOneArr.push(1);
            concatOne = index;
          }
        }
      });
      return {
        one: spanOneArr,
      };
    },
  },
};
</script>
<style scoped lang="scss">
.myDialog .el-form .el-form-item {
  border: none !important;
}


</style>
<style lang="scss">
.noGao {
  .el-table__body tr:hover>td {
    background-color: #0a366d !important;
  }
}
</style>
